package com.jdd.dao.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jdd.common.api.vo.Result;
import com.jdd.common.constant.Send;
import com.jdd.common.util.SnowFlakeUtil;
import com.jdd.common.util.LoginUserUtils;
import com.jdd.dao.entity.TDeviceCmdRequest;
import com.jdd.dao.entity.TDeviceCmdResponse;
import com.jdd.dao.service.TDeviceCmdRequestService;
import com.jdd.dao.service.TDeviceCmdResponseService;
import com.jdd.feign.MqttFegin;
import com.jdd.modules.system.service.ISysDictItemService;
import com.jdd.modules.system.service.ISysDictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author: LS
 * @Date: 2022 - 10 - 25 - 22:07
 * @Description:
 * @version: 1.0
 */
@Slf4j
@RestController("TDeviceCmdResponseController")
@RequestMapping("/cmdLog")
public class TDeviceCmdResponseController {
    @Resource
    public MqttFegin fegin;
    @Resource
    private TDeviceCmdResponseService tDeviceCmdResponseService;
    @Resource
    TDeviceCmdRequestService tDeviceCmdRequestService;

    @Resource
    private ISysDictService dictService;

    @Resource
    private ISysDictItemService dictItemService;

    @GetMapping(value = "/list")
    public Result<IPage<TDeviceCmdResponse>> list(TDeviceCmdResponse tDeviceCmdResponse, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo) {
        Result<IPage<TDeviceCmdResponse>> result = new Result<IPage<TDeviceCmdResponse>>();
        Page<TDeviceCmdResponse> page = new Page<TDeviceCmdResponse>(pageNo, pageSize);
        IPage<TDeviceCmdResponse> iPage = tDeviceCmdResponseService.findList(page,tDeviceCmdResponse);
        if (iPage.getRecords()!=null&&!iPage.getRecords().isEmpty()) {
            for (TDeviceCmdResponse deviceCmdResponse : iPage.getRecords()) {
                String optionType = dictService.queryDictTextByKey("option_type", deviceCmdResponse.getOptionType() + "");
                deviceCmdResponse.setOptionType_dictText(optionType);
            }
        }
        result.setSuccess(true);
        result.setResult(iPage);
        return result;
    }

    @GetMapping(value = "/resendCmd")
    public Result<String> resendCmd(@RequestParam(value = "id") String requestId) {
        Result<String> result = new Result<>();
        TDeviceCmdRequest request = tDeviceCmdRequestService.getById(requestId);
        String cmd = null;
        String clientId = null;
        Integer requestType = null;
        String cmdBatchId = null;
        String createUser = null;
        if (request != null) {
            cmd = request.getCmd();
            clientId = request.getDeviceNumber();
            cmdBatchId = request.getCmdBatchId();
            requestType = request.getRequestType();
        }
        createUser = LoginUserUtils.getLoginUser().getUsername();
        JSONObject jsonObject = JSONObject.parseObject(cmd);
        jsonObject.put("cmd_id", SnowFlakeUtil.getId());
        Send send = new Send();
        send.setCmd(jsonObject.toString());
        send.setCmdBatchId(cmdBatchId);
        send.setMqttType(requestType);
        send.setCreateUser(createUser);
        send.setTopic("face/" + clientId + "/request");
        try {
            fegin.sendMessageWithTopic(send);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("重新发送指令失败");
            return result;
        }
        result.setSuccess(true);
        result.setMessage("重新发送指令成功，待生效！");
        return result;
    }
}
